package com.opensky.module.sys.domain.mybatis.entity;


import com.opensky.module.sys.interfaces.frm.SysMenuQueryFrm;
import com.opensky.common.interfaces.frm.SortFieldFrm;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;
import java.util.Date;

/**
 * 菜单表(表sys_menu)ORM实体类
 *
 * @author 中国印
 * @date 2021-11-27 21:40:33
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "sys_menu")
public class SysMenu {

    /**
     * 主键ID
     */
    @Id
    private Long id;
    /**
     * 父菜单ID，一级菜单为0
     */
    private Long parentId;
    /**
     * 名称
     */
    private String name;
    /**
     * 菜单URL
     */
    private String url;
    /**
     * 菜单类型 0.目录 1.菜单 2.按钮 3.导航菜单
     */
    private Integer type;
    /**
     * 菜单图标
     */
    private String icon;
    /**
     * 排序值，越小越靠前
     */
    private Integer sort;
    /**
     * 状态 0.正常 1.禁用
     */
    private Integer status;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * Frm转Example查询条件
     *
     * @param frm 查询参数对象
     * @return 返回Mybatis所需的查询条件对象
     */
    public static Example getExample(SysMenuQueryFrm frm) {
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        Example example = new Example(SysMenu.class);
        Example.Criteria criteria = example.createCriteria();

        if (frm.getId() != null) {
            criteria.andEqualTo("id", frm.getId());
        }
        if (frm.getParentId() != null) {
            criteria.andEqualTo("parentId", frm.getParentId());
        }
        if (!StringUtils.isEmpty(frm.getName())) {
            criteria.andEqualTo("name", frm.getName());
        }
        if (!StringUtils.isEmpty(frm.getUrl())) {
            criteria.andEqualTo("url", frm.getUrl());
        }
        if (frm.getType() != null) {
            criteria.andEqualTo("type", frm.getType());
        }
        if (!StringUtils.isEmpty(frm.getIcon())) {
            criteria.andEqualTo("icon", frm.getIcon());
        }
        if (frm.getSort() != null) {
            criteria.andEqualTo("sort", frm.getSort());
        }
        if (frm.getStatus() != null) {
            criteria.andEqualTo("status", frm.getStatus());
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("createTime", frm.getCreateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
            criteria.andLessThanOrEqualTo("createTime", frm.getCreateTimeEnd());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("updateTime", frm.getUpdateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
            criteria.andLessThanOrEqualTo("updateTime", frm.getUpdateTimeEnd());
        }
        // 排序
        List<SortFieldFrm> sortFields = frm.getSortFields();
        if (!CollectionUtils.isEmpty(sortFields)) {
            sortFields.forEach(sortField -> {
                if (Sort.Direction.ASC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).asc();
                } else if (Sort.Direction.DESC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).desc();
                }
            });
        } else {
            // 默认按创建时间降序排序
            example.orderBy("createTime").desc();
        }
        return example;
    }

}
